/****************************** (C) COPYRIGHT 2016 Rsestudio ****************************
* File Name          : 
* Author             : RSES_Liyang
* Version            : V1.0.0
* Date               : 00-May-20
* Description        : 
*****************************************************************************************/
/* Includes ----------------------------------------------------------------------------*/
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "Bsp.h"
#include "button.h"
#include "PowerManager.h"
#include "button.h"
#include "main.h"

#include "UI.h"
#include "UI_PowerOff.h"

/* Extern variables --------------------------------------------------------------------*/
/* Private define ----------------------------------------------------------------------*/
/* Private macro -----------------------------------------------------------------------*/
/* Private typedef ---------------------------------------------------------------------*/
/* Private variables -------------------------------------------------------------------*/
static TimerHandle_t ui_timer = NULL;
static StaticTimer_t xui_timer;
static EVENT_SRCEEN event_screen;
static void cb_ui_timer(TimerHandle_t parameter);
static SemaphoreHandle_t mtx_process_event = NULL;     // 互斥量句柄
static StaticSemaphore_t xmtx_process_event;  // 存储互斥量的数据结构

//static uint16_t page_change_timer;
/* Private functions -------------------------------------------------------------------*/
static HMI_ENGINE_RESULT	UI_Initialize(SGUI_SCR_DEV *pDev);
static HMI_ENGINE_RESULT	UI_ExitScreen(SGUI_SCR_DEV *pDev);
static HMI_ENGINE_RESULT	UI_Prepare(SGUI_SCR_DEV *pDev,const void* pParameters);
static HMI_ENGINE_RESULT	UI_Refresh(SGUI_SCR_DEV *pDev,const void* pParameters);
static HMI_ENGINE_RESULT	UI_ProcessEvent(SGUI_SCR_DEV *pDev,const HMI_EVENT_BASE* pEvent,SGUI_INT* pActionID);
static HMI_ENGINE_RESULT	UI_PostProcess(SGUI_SCR_DEV *pDev,HMI_ENGINE_RESULT eProcResult,SGUI_INT ActionID);

static HMI_SCREEN_ACTION	UI_action = 
{
	UI_Initialize,
	UI_Prepare,
	UI_Refresh,
	UI_ProcessEvent,
	UI_PostProcess,
	UI_ExitScreen,
};

HMI_SCREEN_OBJECT		UI_PowerOff = 
{
	UI_SCREEN_ID_PowerOff,
	&UI_action
};

/*****************************************************************************************
* Function Name  : UI_Welcome_Initialize
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
HMI_ENGINE_RESULT	UI_Initialize(SGUI_SCR_DEV *pDev)
{	
	
	mtx_process_event = xSemaphoreCreateMutexStatic(&xmtx_process_event);
	
	ui_timer = xTimerCreateStatic("tm_ui_period", pdMS_TO_TICKS(80), pdTRUE, NULL, cb_ui_timer, &xui_timer);
	
	HMI_EVENT_INIT(event_screen);
	
	return HMI_RET_NORMAL;
}
//

/*****************************************************************************************
* Function Name  : UI_ExitScreen
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
HMI_ENGINE_RESULT	UI_ExitScreen(SGUI_SCR_DEV *pDev)
{	
	xTimerStop(ui_timer, portMAX_DELAY);
	
	//OLED_Clear();
	
	return HMI_RET_NORMAL;
}
//

/*****************************************************************************************
* Function Name  : 
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
HMI_ENGINE_RESULT	UI_Prepare(SGUI_SCR_DEV *pDev,const void* pParameters)
{		
	xTimerStart(ui_timer, portMAX_DELAY);	
	return HMI_RET_NORMAL;
}
//


/*****************************************************************************************
* Function Name  : 
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
HMI_ENGINE_RESULT	UI_Refresh(SGUI_SCR_DEV *pDev,const void* pParameters)
{

	//if(PowerOffRoutine++>10)
	{
//		if(iProfile.Status.VbusOnline != 0)
//		{
//			HMI_SwitchScreen(UI_SCREEN_ID_OffCharge,NULL);
//		}	
//		else
		{
			Power_OFF();
		}
	}

	return HMI_RET_NORMAL;
}
//

/*****************************************************************************************
* Function Name  : 
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
HMI_ENGINE_RESULT	UI_ProcessEvent(SGUI_SCR_DEV *pDev,const HMI_EVENT_BASE* pEvent,SGUI_INT* pActionID)
{
	HMI_ENGINE_RESULT		eProcessResult;
	SGUI_UINT16				KeyValue;
	SGUI_UINT16				KeyAction;
	KEY_PRESS_EVENT			*pKeyEvent;
	SGUI_INT				ProcessAction;
	
	xSemaphoreTake(mtx_process_event, portMAX_DELAY);
	
	eProcessResult = HMI_RET_NORMAL;
	ProcessAction = HMI_DEMO_PROC_NO_ACT;
	
	if(EVENT_TYPE_ACTION == pEvent->iType)
	{
		if(EVENT_ID_KEY_PRESS == pEvent->iID)
		{			
			pKeyEvent = (KEY_PRESS_EVENT*)pEvent;
			KeyValue = EVENT_KEY_ID(pKeyEvent->Data.uiKeyValue);
			KeyAction = EVENT_KEY_ACTION(pKeyEvent->Data.uiKeyValue);	
			
			switch(KeyValue)
			{
				case KEY_UP:
				{
					eProcessResult = HMI_RET_CANCEL_KEY;
					if(KeyAction == BUTTON_EVENT_PRESS)
					{
						
					}				
				}break;
				
				case KEY_DOWN:
				{
					eProcessResult = HMI_RET_CANCEL_KEY;
					if(KeyAction == BUTTON_EVENT_PRESS)
					{								
					}
				}break;
			}
		}
		else if(EVENT_ID_SCREEN == pEvent->iID)
		{
			EVENT_SRCEEN *temp_event;
			temp_event = (EVENT_SRCEEN*)pEvent;
			ProcessAction = HMI_DEMO_PROC_NO_ACT;
					
			if(temp_event->Data == 1)
			{
				UI_Refresh(pDev,NULL);
			}
			
//			if(iProfile.Status.HostConnect == 1)
//			{
//				HMI_SwitchScreen(UI_SCREEN_ID_USBLink,NULL);
//			}
		}
	}
	else if(EVENT_TYPE_DATA == pEvent->iType)
	{
//		if(EVENT_ID_INFOPANNEL == pEvent->iID)
//		{
//			ProcessAction = HMI_DEMO_PROC_NO_ACT;
//			HMI_SwitchScreen(UI_SCREEN_ID_CALIBRATION,&InfoPannel);
//		}
	}
	
	if(pActionID != NULL)
	{
		*pActionID = ProcessAction;
	}
	
	 xSemaphoreGive(mtx_process_event);
	
	return eProcessResult;
}
//

HMI_ENGINE_RESULT	UI_PostProcess(SGUI_SCR_DEV *pDev,HMI_ENGINE_RESULT eProcResult,SGUI_INT ActionID)
{
	if(HMI_PROCESS_SUCCESSFUL(eProcResult))
	{
		if(ActionID!=HMI_DEMO_PROC_NO_ACT)
		{
			//HMI_SwitchScreen(ActionID,NULL);		
		}
		
	}
	return eProcResult;
}
//

/*****************************************************************************************
* Function Name  : cb_timer_autoback
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
static void cb_ui_timer(TimerHandle_t parameter)
{
	event_screen.Head.iType =	EVENT_TYPE_ACTION;
	event_screen.Head.iID 	=	EVENT_ID_SCREEN;
	event_screen.Data = 1;	
	HMI_ProcessEvent((HMI_EVENT_BASE*)&event_screen);
}
//

/********************************(C)COPYRIGHT 2016 RSEStudio ****************************/
/*****************************************END OF FILE************************************/

